#!/usr/bin/env bash

# Detect Linux
linux=false
 case "`uname`" in
  Linux*) linux=true;;
 esac

# Dereference softlinks
THIS="$0"
 while [ -h "$THIS" ] ; do
  ls=`ls -ld "$THIS"`
  softlink=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$softlink" : '/.*' > /dev/null; then THIS="$softlink"; else THIS=`dirname "$THIS"`/"$softlink"; fi
 done
# Detect the installation directory
INSTALLDIR=`dirname "$THIS"`

# Check if FLYWAY_JAVA_CMD is set
if [ -n "$FLYWAY_JAVA_CMD" ]; then
  JAVA_CMD="$FLYWAY_JAVA_CMD"
else
  if [ -x "$INSTALLDIR/jre/bin/java" ]; then
    JAVA_CMD=$INSTALLDIR/jre/bin/java
  else
    # Use JAVA_HOME if it is set
    if [ -z "$JAVA_HOME" ]; then
      JAVA_CMD=java
    else
      JAVA_CMD=$JAVA_HOME/bin/java
    fi
  fi
fi


CP="$CLASSPATH:$INSTALLDIR/lib/*:$INSTALLDIR/lib/aad/*:$INSTALLDIR/lib/plugins/*:$INSTALLDIR/lib/oracle_wallet/*:$INSTALLDIR/lib/flyway/*:$INSTALLDIR/lib/netty/*:$INSTALLDIR/lib/opentelemetry/*:$INSTALLDIR/drivers/*:$INSTALLDIR/drivers/aws/*:$INSTALLDIR/drivers/gcp/*:$INSTALLDIR/drivers/cassandra/*:$INSTALLDIR/drivers/couchbase/*:$INSTALLDIR/drivers/mongo/*"


NATIVE_ACCESS="--enable-native-access=ALL-UNNAMED"
if $linux; then
  EXTRA_ARGS=($NATIVE_ACCESS, -Djava.security.egd=file:/dev/../dev/urandom)
else
  EXTRA_ARGS=($NATIVE_ACCESS, "-Djava.library.path=$INSTALLDIR/native")
fi

if `command -v cygpath > /dev/null`; then CP=`cygpath -pw "$CP"`; fi
"$JAVA_CMD" $JAVA_ARGS "$EXTRA_ARGS" -cp "$CP" org.flywaydb.commandline.Main "$@"

# Exit using the same code returned from Java
exit $?